home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / rpclib / pmap_rmt.c < prev    next >
C/C++ Source or Header  |  1994-03-09  |  13KB  |  458 lines

  1. /*
  2.  * $Id: pmap_rmt.c,v 1.2 1993/11/10 02:25:49 jraja Exp $
  3.  *
  4.  * $Log: pmap_rmt.c,v $
  5.  * Revision 1.2  1993/11/10  02:25:49  jraja
  6.  * Fixed includes + various types.
  7.  * Changed ioctl() and close() to IoctlSocket() and CloseSocket() for AMITCP.
  8.  * Removed EINTR handling for the AMITCP (lets CTRL-C come through).
  9.  * Added AMIGA specific includes.
  10.  * Removed dependency on select() retaining the value of the timeval argument.
  11.  * Changed inet_makeaddr() to Inet_MakeAddr() for the AMITCP, since
  12.  * the return value in amitcp is long.
  13.  * Used FindTask(NULL) (for AMITCP) in place of getpid().
  14.  *
  15.  */
  16. /* @(#)pmap_rmt.c    2.2 88/08/01 4.0 RPCSRC */
  17. /*
  18.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  19.  * unrestricted use provided that this legend is included on all tape
  20.  * media and as a part of the software program in whole or part.  Users
  21.  * may copy or modify Sun RPC without charge, but are not authorized
  22.  * to license or distribute it to anyone else except as part of a product or
  23.  * program developed by the user.
  24.  * 
  25.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  26.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  27.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  28.  * 
  29.  * Sun RPC is provided with no support and without any obligation on the
  30.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  31.  * modification or enhancement.
  32.  * 
  33.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  34.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  35.  * OR ANY PART THEREOF.
  36.  * 
  37.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  38.  * or profits or other special, indirect and consequential damages, even if
  39.  * Sun has been advised of the possibility of such damages.
  40.  * 
  41.  * Sun Microsystems, Inc.
  42.  * 2550 Garcia Avenue
  43.  * Mountain View, California  94043
  44.  */
  45. #if !defined(lint) && defined(SCCSIDS)
  46. static char sccsid[] = "@(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";
  47. #endif
  48.  
  49. /*
  50.  * pmap_rmt.c
  51.  * Client interface to pmap rpc service.
  52.  * remote call and broadcast service
  53.  *
  54.  * Copyright (C) 1984, Sun Microsystems, Inc.
  55.  */
  56.  
  57. #include <sys/param.h>
  58. #include <rpc/rpc.h>
  59. #include <rpc/pmap_prot.h>
  60. #include <rpc/pmap_clnt.h>
  61. #include <rpc/pmap_rmt.h>
  62. #include <sys/socket.h>
  63. #include <stdio.h>
  64. #include <errno.h>
  65. #include <net/if.h>
  66. #include <sys/ioctl.h>
  67. #include <arpa/inet.h>
  68.  
  69. #ifdef AMIGA
  70. #include <proto/exec.h>
  71. #endif
  72.  
  73. #define MAX_BROADCAST_SIZE 1400
  74.  
  75. static struct timeval timeout = { 3, 0 };
  76.  
  77. /*
  78.  * pmapper remote-call-service interface.
  79.  * This routine is used to call the pmapper remote call service
  80.  * which will look up a service program in the port maps, and then
  81.  * remotely call that routine with the given parameters.  This allows
  82.  * programs to do a lookup and call in one step.
  83. */
  84. enum clnt_stat
  85. pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_ptr)
  86.     struct sockaddr_in *addr;
  87.     u_long prog, vers, proc;
  88.     xdrproc_t xdrargs, xdrres;
  89.     caddr_t argsp, resp;
  90.     struct timeval tout;
  91.     u_long *port_ptr;
  92. {
  93.     int socket = -1;
  94.     register CLIENT *client;
  95.     struct rmtcallargs a;
  96.     struct rmtcallres r;
  97.     enum clnt_stat stat;
  98.  
  99.     addr->sin_port = htons(PMAPPORT);
  100.     client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &socket);
  101.     if (client != NULL) {
  102.         a.prog = prog;
  103.         a.vers = vers;
  104.         a.proc = proc;
  105.         a.args_ptr = argsp;
  106.         a.xdr_args = xdrargs;
  107.         r.port_ptr = port_ptr;
  108.         r.results_ptr = resp;
  109.         r.xdr_results = xdrres;
  110.         stat = CLNT_CALL(client, PMAPPROC_CALLIT, xdr_rmtcall_args,
  111.                  (caddr_t)&a, xdr_rmtcallres, (caddr_t)&r,
  112.                  tout);
  113.         CLNT_DESTROY(client);
  114.     } else {
  115.         stat = RPC_FAILED;
  116.     }
  117. #ifdef AMITCP
  118.     (void)CloseSocket(socket);
  119. #else
  120.     (void)close(socket);
  121. #endif
  122.     addr->sin_port = 0;
  123.     return (stat);
  124. }
  125.  
  126.  
  127. /*
  128.  * XDR remote call arguments
  129.  * written for XDR_ENCODE direction only
  130.  */
  131. bool_t XDRFUN
  132. xdr_rmtcall_args(xdrs, cap)
  133.     register XDR *xdrs;
  134.     register struct rmtcallargs *cap;
  135. {
  136.     u_int lenposition, argposition, position;
  137.  
  138.     if (xdr_u_long(xdrs, &(cap->prog)) &&
  139.         xdr_u_long(xdrs, &(cap->vers)) &&
  140.         xdr_u_long(xdrs, &(cap->proc))) {
  141.         lenposition = XDR_GETPOS(xdrs);
  142.         if (! xdr_u_long(xdrs, &(cap->arglen)))
  143.             return (FALSE);
  144.         argposition = XDR_GETPOS(xdrs);
  145.         if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
  146.             return (FALSE);
  147.         position = XDR_GETPOS(xdrs);
  148.         cap->arglen = (u_long)position - (u_long)argposition;
  149.         XDR_SETPOS(xdrs, lenposition);
  150.         if (! xdr_u_long(xdrs, &(cap->arglen)))
  151.             return (FALSE);
  152.         XDR_SETPOS(xdrs, position);
  153.         return (TRUE);
  154.     }
  155.     return (FALSE);
  156. }
  157.  
  158. /*
  159.  * XDR remote call results
  160.  * written for XDR_DECODE direction only
  161.  */
  162. bool_t XDRFUN
  163. xdr_rmtcallres(xdrs, crp)
  164.     register XDR *xdrs;
  165.     register struct rmtcallres *crp;
  166. {
  167.     caddr_t port_ptr;
  168.  
  169.     port_ptr = (caddr_t)crp->port_ptr;
  170.     if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
  171.         xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
  172.         crp->port_ptr = (u_long *)port_ptr;
  173.         return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
  174.     }
  175.     return (FALSE);
  176. }
  177.  
  178.  
  179. /*
  180.  * The following is kludged-up support for simple rpc broadcasts.
  181.  * Someday a large, complicated system will replace these trivial 
  182.  * routines which only support udp/ip .
  183.  */
  184.  
  185. static int
  186. getbroadcastnets(struct in_addr * addrs, int sock, char * buf)
  187. /*    struct in_addr *addrs; */
  188. /*    int sock;  \* any valid socket will do */
  189. /*    char *buf;  \* why allocate more when we can use existing... */
  190. {
  191.     struct ifconf ifc;
  192.         struct ifreq ifreq, *ifr;
  193.     struct sockaddr_in *sin;
  194.         char *cp, *cplim;
  195.         int i = 0;
  196. #ifdef AMITCP
  197. #define ioctl IoctlSocket
  198. #endif
  199.         ifc.ifc_len = UDPMSGSIZE;
  200.         ifc.ifc_buf = buf;
  201.         if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
  202.                 perror("broadcast: ioctl (get interface configuration)");
  203.                 return (0);
  204.         }
  205. #ifndef max
  206. #define max(a, b) (a > b ? a : b)
  207. #endif
  208. #define size(p)    max((p).sa_len, sizeof(p))
  209.     cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
  210.     for (cp = buf; cp < cplim;
  211.          cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
  212.         ifr = (struct ifreq *)cp;
  213.         if (ifr->ifr_addr.sa_family != AF_INET)
  214.             continue;
  215.         ifreq = *ifr;
  216.                 if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
  217.                         perror("broadcast: ioctl (get interface flags)");
  218.                         continue;
  219.                 }
  220.                 if ((ifreq.ifr_flags & IFF_BROADCAST) &&
  221.             (ifreq.ifr_flags & IFF_UP)) {
  222.             sin = (struct sockaddr_in *)&ifr->ifr_addr;
  223. #ifdef SIOCGIFBRDADDR   /* 4.3BSD */
  224.             if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
  225. #ifdef AMITCP
  226.                 addrs[i++].s_addr =
  227.                     Inet_MakeAddr(Inet_NetOf(sin->sin_addr.s_addr),
  228.                           INADDR_ANY);
  229. #else
  230.                 addrs[i++] =
  231.                     inet_makeaddr(inet_netof(sin->sin_addr),
  232.                     INADDR_ANY);
  233. #endif
  234.             } else {
  235.                 addrs[i++] = ((struct sockaddr_in*)
  236.                   &ifreq.ifr_addr)->sin_addr;
  237.             }
  238. #else /* 4.2 BSD */
  239.             addrs[i++] = inet_makeaddr(inet_netof(sin->sin_addr),
  240.                 INADDR_ANY);
  241. #endif
  242.         }
  243.     }
  244. #ifdef AMITCP
  245. #undef ioctl
  246. #endif
  247.     return (i);
  248. }
  249.  
  250. enum clnt_stat 
  251. clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
  252.     u_long        prog;        /* program number */
  253.     u_long        vers;        /* version number */
  254.     u_long        proc;        /* procedure number */
  255.     xdrproc_t    xargs;        /* xdr routine for args */
  256.     caddr_t        argsp;        /* pointer to args */
  257.     xdrproc_t    xresults;    /* xdr routine for results */
  258.     caddr_t        resultsp;    /* pointer to results */
  259.     resultproc_t    eachresult;    /* call with each result obtained */
  260. {
  261.     enum clnt_stat stat;
  262.     AUTH *unix_auth = authunix_create_default();
  263.     XDR xdr_stream;
  264.     register XDR *xdrs = &xdr_stream;
  265.     int outlen, inlen, nets;
  266.     register int sock;
  267. #ifdef AMITCP
  268.     long fromlen;
  269.     long on = 1;
  270. #else
  271.     int fromlen;
  272.     int on = 1;
  273. #endif
  274. #ifdef FD_SETSIZE
  275.     fd_set mask;
  276.     fd_set readfds;
  277. #else
  278.     int readfds;
  279.     register int mask;
  280. #endif /* def FD_SETSIZE */
  281.     register int i;
  282.     bool_t done = FALSE;
  283.     register u_long xid;
  284.     u_long port;
  285.     struct in_addr addrs[20];
  286.     struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
  287.     struct rmtcallargs a;
  288.     struct rmtcallres r;
  289.     struct rpc_msg msg;
  290. #ifdef AMITCP
  291.     struct timeval t, t_temp; 
  292. #else
  293.     struct timeval t;
  294. #endif 
  295.     char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
  296.  
  297.     /*
  298.      * initialization: create a socket, a broadcast address, and
  299.      * preserialize the arguments into a send buffer.
  300.      */
  301.     if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  302.         perror("Cannot create socket for broadcast rpc");
  303.         stat = RPC_CANTSEND;
  304.         goto done_broad;
  305.     }
  306. #ifdef SO_BROADCAST
  307.     if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (caddr_t)&on, sizeof (on)) < 0) {
  308.         perror("Cannot set socket option SO_BROADCAST");
  309.         stat = RPC_CANTSEND;
  310.         goto done_broad;
  311.     }
  312. #endif /* def SO_BROADCAST */
  313. #ifdef FD_SETSIZE
  314.     FD_ZERO(&mask);
  315.     FD_SET(sock, &mask);
  316. #else
  317.     mask = (1 << sock);
  318. #endif /* def FD_SETSIZE */
  319.     nets = getbroadcastnets(addrs, sock, inbuf);
  320.     bzero((char *)&baddr, sizeof (baddr));
  321.     baddr.sin_family = AF_INET;
  322.     baddr.sin_port = htons(PMAPPORT);
  323.     baddr.sin_addr.s_addr = htonl(INADDR_ANY);
  324. /*    baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
  325.     (void)gettimeofday(&t, (struct timezone *)0);
  326. #ifdef AMIGA
  327.     msg.rm_xid = xid = (u_long)FindTask(NULL) ^ t.tv_sec ^ t.tv_usec;
  328. #else
  329.     msg.rm_xid = xid = getpid() ^ t.tv_sec ^ t.tv_usec;
  330. #endif
  331.     t.tv_usec = 0;
  332.     msg.rm_direction = CALL;
  333.     msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
  334.     msg.rm_call.cb_prog = PMAPPROG;
  335.     msg.rm_call.cb_vers = PMAPVERS;
  336.     msg.rm_call.cb_proc = PMAPPROC_CALLIT;
  337.     msg.rm_call.cb_cred = unix_auth->ah_cred;
  338.     msg.rm_call.cb_verf = unix_auth->ah_verf;
  339.     a.prog = prog;
  340.     a.vers = vers;
  341.     a.proc = proc;
  342.     a.xdr_args = xargs;
  343.     a.args_ptr = argsp;
  344.     r.port_ptr = &port;
  345.     r.xdr_results = xresults;
  346.     r.results_ptr = resultsp;
  347.     xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
  348.     if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) {
  349.         stat = RPC_CANTENCODEARGS;
  350.         goto done_broad;
  351.     }
  352.     outlen = (int)xdr_getpos(xdrs);
  353.     xdr_destroy(xdrs);
  354.     /*
  355.      * Basic loop: broadcast a packet and wait a while for response(s).
  356.      * The response timeout grows larger per iteration.
  357.      */
  358.     for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
  359.         for (i = 0; i < nets; i++) {
  360.             baddr.sin_addr = addrs[i];
  361.             if (sendto(sock, outbuf, outlen, 0,
  362.                 (struct sockaddr *)&baddr,
  363.                 sizeof (struct sockaddr)) != outlen) {
  364.                 perror("Cannot send broadcast packet");
  365.                 stat = RPC_CANTSEND;
  366.                 goto done_broad;
  367.             }
  368.         }
  369.         if (eachresult == NULL) {
  370.             stat = RPC_SUCCESS;
  371.             goto done_broad;
  372.         }
  373.     recv_again:
  374.         msg.acpted_rply.ar_verf = _null_auth;
  375.         msg.acpted_rply.ar_results.where = (caddr_t)&r;
  376.                 msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_rmtcallres;
  377.         readfds = mask;
  378. #ifdef AMITCP
  379.         switch (select(_rpc_dtablesize(), &readfds, NULL, 
  380.                    NULL, &(t_temp = t)))
  381. #else
  382.         switch (select(_rpc_dtablesize(), &readfds, NULL,
  383.                    NULL, &t))
  384. #endif
  385.           {
  386.         case 0:  /* timed out */
  387.             stat = RPC_TIMEDOUT;
  388.             continue;
  389.  
  390.         case -1:  /* some kind of error */
  391. #ifndef AMITCP /* EINTR is returned in case of a CTRL-C by default */
  392.             if (errno == EINTR)
  393.                 goto recv_again;
  394. #endif
  395.             perror("Broadcast select problem");
  396.             stat = RPC_CANTRECV;
  397.             goto done_broad;
  398.  
  399.         }  /* end of select results switch */
  400.     try_again:
  401.         fromlen = sizeof(struct sockaddr);
  402.         inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
  403.             (struct sockaddr *)&raddr, &fromlen);
  404.         if (inlen < 0) {
  405. #ifndef AMITCP /* EINTR is returned in case of a CTRL-C by default */
  406.             if (errno == EINTR)
  407.                 goto try_again;
  408. #endif
  409.             perror("Cannot receive reply to broadcast");
  410.             stat = RPC_CANTRECV;
  411.             goto done_broad;
  412.         }
  413.         if (inlen < sizeof(u_long))
  414.             goto recv_again;
  415.         /*
  416.          * see if reply transaction id matches sent id.
  417.          * If so, decode the results.
  418.          */
  419.         xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
  420.         if (xdr_replymsg(xdrs, &msg)) {
  421.             if ((msg.rm_xid == xid) &&
  422.                 (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
  423.                 (msg.acpted_rply.ar_stat == SUCCESS)) {
  424.                 raddr.sin_port = htons((u_short)port);
  425.                 done = (*eachresult)(resultsp, &raddr);
  426.             }
  427.             /* otherwise, we just ignore the errors ... */
  428.         } else {
  429. #ifdef notdef
  430.             /* some kind of deserialization problem ... */
  431.             if (msg.rm_xid == xid)
  432.                 fprintf(stderr, "Broadcast deserialization problem");
  433.             /* otherwise, just random garbage */
  434. #endif
  435.         }
  436.         xdrs->x_op = XDR_FREE;
  437.         msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
  438.         (void)xdr_replymsg(xdrs, &msg);
  439.         (void)(*xresults)(xdrs, resultsp);
  440.         xdr_destroy(xdrs);
  441.         if (done) {
  442.             stat = RPC_SUCCESS;
  443.             goto done_broad;
  444.         } else {
  445.             goto recv_again;
  446.         }
  447.     }
  448. done_broad:
  449. #ifdef AMITCP
  450.     (void)CloseSocket(sock);
  451. #else
  452.     (void)close(sock);
  453. #endif
  454.     AUTH_DESTROY(unix_auth);
  455.     return (stat);
  456. }
  457.  
  458.